package com.daou.smartpush.domain.company;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.daou.smartpush.domain.license.License;

@Entity
@Table(name = "company")
public class Company implements Serializable {

    private static final long serialVersionUID = -3708198666091003519L;

    @Id
    @Column(name = "companySeq", nullable = false)
    @GeneratedValue
    private Long companySeq;

    @Column(name = "companyName", nullable = false, unique = true)
    private String companyName;

    @Column(name = "tel1")
    private String tel1;

    @Column(name = "tel2")
    private String tel2;

    @Column(name = "tel3")
    private String tel3;

    @Column(name = "email")
    private String email;

    @Column(name = "created")
    private Date created;

    @Column(name = "modified")
    private Date modified;

    @OneToMany(mappedBy = "company", cascade = {CascadeType.ALL})
    List<License> info;

    @Transient
    private String error_msg;

    public Long getCompanySeq() {
        return companySeq;
    }

    public void setCompanySeq(Long companySeq) {
        this.companySeq = companySeq;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getTel1() {
        return tel1;
    }

    public void setTel1(String tel1) {
        this.tel1 = tel1;
    }

    public String getTel2() {
        return tel2;
    }

    public void setTel2(String tel2) {
        this.tel2 = tel2;
    }

    public String getTel3() {
        return tel3;
    }

    public void setTel3(String tel3) {
        this.tel3 = tel3;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getModified() {
        return modified;
    }

    public void setModified(Date modified) {
        this.modified = modified;
    }

    @PrePersist
    public void onPrePersist() {
        this.created = new Date();
        this.modified = this.created;
    }

    @PreUpdate
    public void onPreUpdate() {
        this.modified = new Date();
    }

    public String getError_msg() {
        return error_msg;
    }

    public void setError_msg(String error_msg) {
        this.error_msg = error_msg;
    }

    public List<License> getLicenses() {
        return info;
    }

    public void setInfo(List<License> info) {
        this.info = info;
    }

    public void addLicense(License license) {
        if (this.getLicenses().contains(license))
            return;
        this.getLicenses().add(license);
        if (license.getCompany() != null) {
            if (license.getCompany().getLicenses().remove(license))
                ;
        }
        license.setCompany(this);

    }

}
